package com.dearbinge.data.service.impl.commodity;

import com.dearbinge.data.api.commodity.CommodityMessageService;
import com.dearbinge.data.dao.CommodityMessageMapper;
import com.dearbinge.data.dto.commodity.CommodityMessageDto;
import com.dearbinge.data.pojo.commodity.CommodityMessage;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.date.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service(value = "commodityMessageService")
public class CommodityMessageServiceImpl implements CommodityMessageService {

    @Autowired
    private CommodityMessageMapper commodityMessageMapper;

    @Override
    public Integer insert(CommodityMessage commodityMessage) {
        Integer dateCode = commodityMessageMapper.insert(commodityMessage);
        return dateCode;
    }

    @Override
    public Integer update(CommodityMessage commodityMessage) {
        Integer dateCode = commodityMessageMapper.update(commodityMessage);
        return dateCode;
    }

    @Override
    public List<CommodityMessageDto> getAll() {
       List<CommodityMessage> cmList = commodityMessageMapper.getAll();
        List<CommodityMessageDto> resultMsg = new ArrayList<CommodityMessageDto>();
       if (cmList != null){
           resultMsg = resultMsg(cmList);
       }
       return resultMsg;
    }

    @Override
    public PageResult<CommodityMessageDto> getByAsk(String keyWords, Boolean isDeleted, Integer pageNo, Integer pageSize) {
        pageNo = (null == pageNo || pageNo < 1) ? 1 : pageNo;
        pageSize = (null == pageSize || pageSize < 0) ? 10 :pageSize;
        PageHelper.startPage(pageNo,pageSize,String.format("%s %s","create_by","DESC"));
        if (StringUtils.isNoneBlank(keyWords)){
            keyWords = "%"+keyWords+"%";
        }
        List<CommodityMessage> cmList = commodityMessageMapper.getByAsk(keyWords,isDeleted);
        List<CommodityMessageDto> dtoList = new ArrayList<CommodityMessageDto>();
        if (cmList != null){
            dtoList = resultMsg(cmList);
        }
        PageInfo<CommodityMessageDto> pageInfo = new PageInfo<CommodityMessageDto>(dtoList);
        return new PageResult<CommodityMessageDto>(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public CommodityMessageDto getById(Integer id) {
       CommodityMessage commodityMessage =  commodityMessageMapper.getById(id);
        List<CommodityMessage> list = new ArrayList<CommodityMessage>();
        List<CommodityMessageDto> dtoList = new ArrayList<CommodityMessageDto>();
        if (commodityMessage != null) {
           list.add(commodityMessage);
           dtoList = resultMsg(list);
       }
        return dtoList.get(0);
    }


    /**
     * 公共日期转换方法
     * @param list
     * @return
     */
    public List<CommodityMessageDto> resultMsg(List<CommodityMessage> list){
        CommodityMessage commodityMessage = new CommodityMessage();
        List<CommodityMessageDto> dtoList = new ArrayList<CommodityMessageDto>();
        for (int index = 0;index < list.size();index++){
            CommodityMessageDto dto = new CommodityMessageDto();
            commodityMessage = list.get(index);
            dto.setCommodityId(commodityMessage.getCommodityId());
            dto.setContent(commodityMessage.getContent());
            dto.setCreateBy(commodityMessage.getCreateBy());
            dto.setModifyBy(commodityMessage.getModifyBy());
            dto.setCreateTime(DateUtils.date2String(commodityMessage.getCreateTime()));
            dto.setModifyTime(DateUtils.date2String(commodityMessage.getModifyTime()));
            dto.setUserId(commodityMessage.getUserId());
            dto.setDeleted(commodityMessage.getIsDeleted());
            dtoList.add(dto);
        }

        return dtoList;
    }
}
